package com.stx.system;


import java.util.*;
import java.util.regex.Pattern;

//学生业务操作做 负责对学生信息的增删改查
public class StudentService {

    static Scanner sc = new Scanner(System.in);
    private static Object Student;


    //根据提示获取键盘输入字符串
    public static String getInputStrByTip(String tip) {
        System.out.println(tip);
        return sc.next();
    }

    public static void addStudent() {

        String stuNo = null;
        boolean flag;

        while (true) {
            flag = false; //  默认是没有相同的ID
            stuNo = getInputStrByTip("请输入学号：");
            //去重学号
            for (Student s1 : DB.studentList) {
                if (s1.getStuNo().equals(stuNo)) {
                    System.out.println("您输入的学生学号已存在,请重新输入!!");
                    flag = true;
                }
            }
            if (!flag && stuNo.length() == 5 && Display.parseToInt(stuNo)) {
                System.out.println("恭喜您输入了正确的学生学号");
                break;
            } else if (!flag) {
                System.out.println("学生ID没有重复,但是格式错误,只能是五位数的数字形式");
            }
        }
        String stuName = getInputStrByTip("请输入姓名：");
        String stuSex = "";
        // 判断性别输入:


        // 判断年龄
        int age = 0;
        ;

        String stuAddr = getInputStrByTip("请输入家庭住址：");

        double stuScore = 0;
        ;


        String stuHobby = getInputStrByTip("请输入兴趣爱好：");


        String stuPhone = "";

        int start = DB.studentList.size();
        Student newStu = new Student(stuNo, stuName, uTools.deterSex(stuSex), uTools.deterAge(age), stuAddr, uTools.determineScore(stuScore), stuHobby, uTools.deterStuNum(stuPhone));
        //将新的学生对象添加学生列表中
        DB.studentList.add(newStu);
        int end = DB.studentList.size();

        if (end > start) {
            System.out.println("恭喜~~添加学生成功！");
        } else {
            System.out.println("抱歉~~添加学生失败！");
        }

    }

    // 正则校验格式是否正确
    public static boolean isValidPhoneNumber(String phoneNumber) {
        String pattern = "^((13[0-9])|(14[5-9])|(15[0-3,5-9])|(16[6])|(17[0-8])|(18[0-9])|(19[8|9]))\\d{8}$";
        return Pattern.matches(pattern, phoneNumber);
    }

    //判断学生电话号码是否重复
    public static boolean stuPhoneRepeat(String stuPho) {
        for (int i = 0; i < DB.studentList.size(); i++) {
            if (DB.studentList.get(i).getStuPhone().equals(stuPho)) {
                return true;
            }
        }
        return false;
    }

    public static void queryAllStudents() {

        if (DB.studentList.size() == 0) {
            System.out.println("学生列表为空，请添加学生信息！");
        } else {
            ascendingOrderByStu(DB.studentList);
            System.out.println("--------------学生列表如下-----------");
            for (Student student : DB.studentList) {
                System.out.println(student);
            }
        }

    }

    public static void queryStudentById() {

        String stuNo = getInputStrByTip("请输入要查询学生的学号：");
        //记录查询到学生信息
        Student selectStu = null;
        for (Student stu : DB.studentList) {
            //遍历到该学生，将学生信息记录到selectStu中
            if (stu.getStuNo().equals(stuNo)) {
                selectStu = stu;
                break;
            }
        }
        if (selectStu != null) {

            System.out.println("----------------------查询学生信息如下-------------------");
            System.out.println(selectStu);

        } else {
            System.out.println("查询结果为空，不存在该学生！");
        }
    }

    public static void queryStudentByName() {

        //创建一个目标数组，来存放同名的学生
        ArrayList<Student> selectStus = new ArrayList<>();
        String stuName = getInputStrByTip("请输入要查询学生的姓名：");
        for (Student stu : DB.studentList) {

            if (stu.getStuName().equals(stuName)) {
                //将同名学生对象添加到数组中
                selectStus.add(stu);
            }
        }
        if (selectStus.size() == 0) {
            System.out.println("查询结果为空，未查询到该学生！");
        } else {
            System.out.println("***********************查询结果如下***********************");
            for (Student stu : selectStus) {
                System.out.println(stu);
            }
        }
    }

    public static void updateStudentById() {

        //定义存放要更新学生对象的变量
        Student updateStu = null;
        String stuNo = getInputStrByTip("请输入要更新的学生学号：");
        //找要更新学生信息
        for (Student stu : DB.studentList) {
            if (stu.getStuNo().equals(stuNo)) {
                updateStu = stu;
                break;
            }
        }
        if (updateStu != null) {
            String newName = getInputStrByTip("请输入要更新姓名：");
            updateStu.setStuName(newName);

            String newSex = "";
            updateStu.setStuSex(uTools.deterSex(newSex));

            double score = 0;
            updateStu.setStuScore(uTools.determineScore(score));

            int age = 0;
            updateStu.setStuAge(uTools.deterAge(age));

            String stuAddr = getInputStrByTip("请输入更新后的地址:");
            updateStu.setStuAddr(stuAddr);

            String stuHobby = getInputStrByTip("请输入更新后的学生爱好:");
            updateStu.setStuHobby(stuHobby);



            //对updateStu进行更新
            // updateStu.setStuName(newName);
            // updateStu.setStuSex(newSex);
            // updateStu.setStuAge(Integer.parseInt(newAge));
            updateStu.setStuAge(age);
            System.out.println("学号为" + stuNo + "的学生信息更新成功");

        } else {
            System.out.println("不存在该生，更新失败");
        }
    }

    public static void deleteStudentById() {

        //创建一个要记录的删除对象
        Student delStu = null;
        String stuNo = getInputStrByTip("请输入要删除学生的学号：");
        //找目标对象
        for (Student stu : DB.studentList) {

            if (stu.getStuNo().equals(stuNo)) {
                delStu = stu;
                break;
            }
        }
        //对目标处理
        if (delStu == null) {
            System.out.println("不存在该学生，删除失败！");
        } else {
            DB.studentList.remove(delStu);
            System.out.println("恭喜~删除成功！");
        }
    }

    public static void deleteAllStudents() {

        //    必须存在学生信息;
        if (DB.studentList.size() == 0) {
            System.out.println("很抱歉,无法删除学生信息,因为此时的学生信息列表为空");
        } else {
            System.out.println("你确定要删除所有的学生信息吗??这很危险!!");
            System.out.println("其他:取消  1:确定");
            String inputNum = sc.next();
            try {
                if (inputNum.equals("1")) {
                    DB.studentList.clear();
                    System.out.println("已成功删除所有学生信息");
                } else {
                    System.out.println("取消删除学生");
                    return;
                }
            } catch (InputMismatchException e) {
                e.printStackTrace();
                System.out.println("请输入正确的数据.");
                return;
            }
        }
    }

    public static void scoreSort() {
        System.out.println("请输入您想要排序的方式");
        System.out.println("1: 降序, 其他: 升序");
        String way = sc.next();
        if (way.equals("1")) {
            System.out.println("降序:");
            descendingOrder(DB.studentList);
            for (Student student : DB.studentList) {
                System.out.println(student.getStuName() + ": " + student.getStuScore());
            }
            // DB.studentList.
        } else {
            System.out.println("升序:");
            ascendingOrder(DB.studentList);
            for (Student student : DB.studentList) {
                System.out.println(student.getStuName() + ": " + student.getStuScore());
            }
        }

    }

    public static void scoreFailMenu() {
        ArrayList<com.stx.system.Student> failStu = new ArrayList<>();
        for (int i = 0; i < DB.studentList.size(); i++) {
            if (DB.studentList.get(i).getStuScore() < 60) {
                failStu.add(DB.studentList.get(i));
            }
        }
        if (failStu.size() > 0) {
            System.out.println("=============不及格学生名单如下:=============");
            for (com.stx.system.Student s1 : failStu) {
                System.out.println(s1);
            }
        } else {
            System.out.println("非常幸运,此次没有不及格的学生");
        }
    }

    //    降序排序
    public static void descendingOrder(ArrayList<Student> students) {
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return Double.compare(s2.getStuScore(), s1.getStuScore()); // 降序排序
            }
        });
    }

    //升序排序
    public static void ascendingOrder(ArrayList<Student> students) {
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return Double.compare(s1.getStuScore(), s2.getStuScore()); // 升序排序
            }
        });
    }

    //    根据学生的学号来一个升序的排序
    public static void ascendingOrderByStu(ArrayList<Student> students) {
        // 根据ID进行升序排序
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                return s1.getStuNo().compareTo(s2.getStuNo());
            }
        });
    }
}
